//package club.baimahu.pencil.miaosha.rocketmq;
//
//import club.baimahu.pencil.miaosha.domain.StockOrder;
//import club.baimahu.pencil.miaosha.service.StockService;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
//import org.apache.rocketmq.common.message.MessageExt;
//import org.apache.rocketmq.spring.annotation.MessageModel;
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
//import org.apache.rocketmq.spring.core.RocketMQListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.Message;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Service;
//
//import java.io.UnsupportedEncodingException;
//import java.util.Date;
//
///**
// * TODO
// *
// * @author zhanghongjiang
// * @version 1.0
// * @date 2021/9/17 15:13
// */
//@Component
//@Slf4j
//public class RocketServiceListener {
//
//
//    @Autowired
//    StockService stockService;
//
//    @Service
//    @RocketMQMessageListener(consumerGroup = "consumer-group-1", topic = "rocket-topic-2")
//    public class consumeCreateOrder implements RocketMQListener<Message> {
//        @Override
//        public void onMessage(Message  s) {
//            log.info("开始消费消息[{}]", StockService.CREATE_STOCK_ORDER_TOPIC);
//                try {
//                    String body = s.getPayload().toString();
//                    if(body != null){
//                        String[] arr = body.split(";");
//                        if(arr.length != 3 || !arr[0].matches("[0-9]+")){
//                            log.warn("消息格式不符合规范,忽略该消息,body = {}",body);
//                        }
//                        Integer sid = Integer.parseInt(arr[0]);
//                        Integer userId = Integer.parseInt(arr[2]);
//                        String name = arr[1];
//
//                        StockOrder stockOrder = new StockOrder();
//                        String keys = s.getHeaders().get("KEYS").toString();
//                        stockOrder.setId(Long.parseLong(keys));
//                        stockOrder.setSid(sid);
//                        stockOrder.setName(name);
//                        stockOrder.setUserId(userId);
//                        stockOrder.setCreateTime(new Date());
//                        stockOrder.setTxId(keys);
//
//                        int affect = stockService.createStockOrder(stockOrder);
//                        if(affect == 1){
//                            log.info("创建订单成功,stockOrder={}",stockOrder.toString());
//                            //return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//                        }else{
//                            log.error("创建订单失败,stockOrder={}",stockOrder.toString());
//                        }
//                    }
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                } catch (Exception e){
//                    log.error("消费消息出现异常,message : {}" , e.getLocalizedMessage());
//                    //可发送邮件短信进行相应报警
//                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
//                }
//            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//        }
//    }
//
//
//
//
//    // RocketMQ支持两种消费方式，集器消费和广播消费
//    @Service
//    @RocketMQMessageListener(consumerGroup = "consumer-group-2", topic = "rocket-topic-2",
//            selectorExpression = "tag2", messageModel = MessageModel.BROADCASTING)
//    public class Consumer2 implements RocketMQListener<String> {
//        @Override
//        public void onMessage(String s) {
//            log.info("consumer2 rocket收到消息：{}", s);
//        }
//    }
//}
